package com.qianfeng.lingshijia;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import adapter.ViewPageAdapter;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;


public class DetailsActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    @ViewInject(R.id.id_rg_detatils)
    private RadioGroup group;
    @ViewInject(R.id.id_vp_details)
    private ViewPager vp;

    private int productId;

    private boolean flag = false;

    private int num=1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {

        Intent intent = getIntent();
        productId = intent.getIntExtra("productId", 0);
        //menu设置监听
        group.setOnCheckedChangeListener(this);

        RadioButton rb_details = (RadioButton) findViewById(R.id.id_rb_details);
        rb_details.setChecked(true);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) group.getChildAt(position);
                rb.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager(),productId);
        vp.setAdapter(adapter);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.id_rb_details:
                vp.setCurrentItem(0);
                break;
            case R.id.id_rb_more:
                vp.setCurrentItem(1);
                break;
            case R.id.id_rb_type:
                vp.setCurrentItem(2);
                break;
            default:
                break;
        }

    }

    public void detailback(View view){
        this.finish();
    }

    public void showOneShare(View view)
    {
        ShareSDK.initSDK(this);

        OnekeyShare oks = new OnekeyShare();

        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        oks.setTitle("格林豪泰酒店上海浦东分店");

        oks.setText("格林豪泰酒店上海浦东分店格林豪泰酒店上海浦东分店" +
                "格林豪泰酒店上海浦东分店格林豪泰酒店上海浦东分店");

        //oks.setImagePath();
        oks.setImageUrl("http://a3.greentree.cn:8022/UploadFiles/CRS/HotelImg/010003/Logo/Logo_010003_1.jpg");

        oks.setTitleUrl("http://sharesdk.cn");

        oks.show(this);
    }

    public void addshopping(View view){
        flag = false;
        makeDialog();
    }

    public void buyNow(View view){
        flag = true;
        makeDialog();
    }

    private void makeDialog(){
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.buy_window_layout);
        Window dialogWindow = dialog.getWindow();

        WindowManager w = getWindowManager();
        Display d = w.getDefaultDisplay();
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        p.width = WindowManager.LayoutParams.MATCH_PARENT;
        p.horizontalMargin = 0;
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);

        Button btn = (Button)dialog.findViewById(R.id.id_surebuy);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag) {
                    Intent intent = new Intent();
                    intent.setClass(getApplicationContext(), IndentActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getApplicationContext().startActivity(intent);
                    dialog.dismiss();
                } else {
                    Toast.makeText(getApplicationContext(), "添加购物车成功", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }
        });

        ImageView iv1 = (ImageView) dialog.findViewById(R.id.id_add);
        ImageView iv2 = (ImageView) dialog.findViewById(R.id.id_reduce);
        EditText editText = (EditText)dialog.findViewById(R.id.id_et_num);

        num=1;
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num++;
            }
        });
        iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(num>1){
                    num--;

                }
            }
        });
        editText.setText(num + "");
        invalidateOptionsMenu();
        dialog.show();
    }



}
